﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designer.Event.UI;
using System.ComponentModel;
using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.UI;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// SolutionWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SceneWindow : Window
    {
        private Guid _SolutionId;

        private string _name;
        private string _caption;
        private double _width, _height;
        private bool _isShowNavigate, _isAllowSwapArea;
        public SceneWindow()
        {
            InitializeComponent();

            this.Closed += SceneWindow_Closed;
        }

        public SceneWindow(Guid solutionId)
        {
            InitializeComponent();

            _SolutionId = solutionId;
            this.Closed += SceneWindow_Closed;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tbName.Text = "场景" + (++UIContext.Current.CurrentSolution.IdSeed);
            tbTitle.Text = "场景" + UIContext.Current.CurrentSolution.IdSeed;

            NCLEventBus.Register(this);
        }
        void SceneWindow_Closed(object sender, EventArgs e)
        {
            NCLEventBus.Unregister(this);
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbName.Text))
            {
                CustomMessageBox.ShowError(string.Format("{0}的{1}不正确", tbName.Name, tbName.Text));
                return;
            }
            if (string.IsNullOrWhiteSpace(tbTitle.Text))
            {
                CustomMessageBox.ShowError(string.Format("{0}的{1}不正确", tbTitle.Name, tbTitle.Text));
                return;
            }
            if (string.IsNullOrWhiteSpace(tbWidth.Text))
            {
                CustomMessageBox.ShowError(string.Format("{0}的{1}不正确", tbWidth.Name, tbWidth.Text));
                return;
            }
            else
            {
                if (!double.TryParse(tbWidth.Text, out _width))
                {
                    CustomMessageBox.ShowError(string.Format("{0}的{1}不正确", tbWidth.Name, tbWidth.Text));
                    return;
                }
                else if(_width > 65536)
                {
                    CustomMessageBox.ShowError(string.Format("{0}的{1}过大", tbWidth.Name, tbWidth.Text));
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(tbHeight.Text))
            {
                CustomMessageBox.ShowError(string.Format("{0}的{1}不正确", tbHeight.Name, tbHeight.Text));
                return;
            }
            else
            {
                if (!double.TryParse(tbHeight.Text, out _height))
                {
                    CustomMessageBox.ShowError(string.Format("{0}的{1}不正确", tbHeight.Name, tbHeight.Text));
                    return;
                }
                else if (_height > 10000)
                {
                    CustomMessageBox.ShowError(string.Format("{0}的{1}过大", tbHeight.Name, tbHeight.Text));
                    return;
                }
            }
            if (UIContext.Current.CurrentSolution.ExistScene(tbName.Text))
            {
                CustomMessageBox.ShowError(string.Format("{0}场景名称已存在", tbName.Text));
                return;
            }
            this.IsEnabled = false;
            NCLEventBus.Publish(new BeginGetLatestVersionEvent(UIContext.Current.CurrentSolution.ID, UIContext.Current.CurrentSolution.ID,UIContext.Current.Token));
        }
        private void BeginCreateScene()
        {
            _name = tbName.Text;
            _caption = tbTitle.Text;
            _isShowNavigate = cbShowNavigate.SelectedIndex == 0 ? true : false;
            _isAllowSwapArea = cbAllowSwap.SelectedIndex == 0 ? true : false;

            if (UIContext.Current.SolutionNode != null && (UIContext.Current.SolutionNode.NodeStatus == NodeStatus.CheckOut || UIContext.Current.SolutionNode.NodeStatus == NodeStatus.Add))
            {
                if (UIContext.Current.CurrentSelectedNode.NodeType != NodeType.Scene)
                {
                    CreateScene();
                    return;
                }
                else if (UIContext.Current.CurrentSelectedNode.NodeStatus == NodeStatus.CheckOut || UIContext.Current.CurrentSelectedNode.NodeStatus == NodeStatus.Add)
                {
                    CreateScene();
                    return;
                }
            }
            List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
            nodeList.Add(UIContext.Current.SolutionNode);

            CheckOutThread.Start(nodeList, ReportProgress, Completed);
            ProgressbarWindow.Instance.ShowProgress(false);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public void ReportProgress(Guid id, string name, int current, int total)
        {
            Dispatcher.Invoke(() =>
            {
                ProgressbarWindow.Instance.ReportProgress(current, total);
            });
        }

        public void Completed(bool HasError, int successed, int total)
        {
            ProgressbarWindow.Instance.Complete();
            if (HasError)
            {
                this.Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowError("方案签出失败");
                });
                return;
            }
            CreateScene();
        }

        private void CreateScene()
        {
            UIEventBus.Publish(new UICreateSceneEvent(_name, _caption, _isShowNavigate, _height, _width, _isAllowSwapArea));
            Logger.DebugToTag(Library.LogTAG, string.Format("name:{0},caption:{1},isshowNavigate:{2},height:{3},width:{4},isallowswaparea:{5}", _name, _caption, _isShowNavigate, _height, _width, _isAllowSwapArea));
            this.Dispatcher.Invoke(() =>
            {
                this.DialogResult = true;
            });
        }
    }
}
